package com.zenithmind.gateway.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Mono;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.List;
import java.util.HashMap;

/**
 * 网关通用配置类
 * 包含网关限流、路由映射等配置
 */
@Slf4j
@Data
@Configuration
@ConfigurationProperties(prefix = "zenithmind.gateway")
public class GatewayConfig {

    /**
     * 服务路由映射配置
     * 格式为: {服务名: [API路径前缀列表]}
     */
    private Map<String, List<String>> serviceRoutes = new HashMap<>();
    
    /**
     * 获取默认路由配置
     * 当配置文件中没有定义时使用这些默认值
     * 注意：随着路由配置迁移到RouteConfiguration，此方法已不是必需的
     */
    public Map<String, List<String>> getServiceRoutesWithDefaults() {
        if (serviceRoutes == null || serviceRoutes.isEmpty()) {
            Map<String, List<String>> defaults = new HashMap<>();
            
            // 1. 用户相关服务默认路由
            defaults.put("user-service", 
                List.of("user", "auth", "verification", "profile", "role", "permission", "dashboard", "stats"));
                
            // 2. 内容管理服务默认路由
            defaults.put("article-service", 
                List.of("article", "article-category", "category", "tag"));
                
            defaults.put("note-service", 
                List.of("note", "notebook", "note-tag"));
                
            defaults.put("news-service", 
                List.of("news", "announcement"));
                
            defaults.put("document-service", 
                List.of("document", "template", "doc"));
                
            defaults.put("mall-service", 
                List.of("mall", "product", "order", "payment"));
                
            // 3. 教育学习服务默认路由
            defaults.put("classroom-service", 
                List.of("course", "courses", "chapter", "lesson"));
                
            defaults.put("exam-service", 
                List.of("exam", "question", "record"));
                
            defaults.put("coding-service", 
                List.of("problem", "submission", "code"));
                
            defaults.put("library-service", 
                List.of("library", "book", "borrow"));
                
            // 4. 存储服务默认路由
            defaults.put("disk-service", 
                List.of("disk", "file", "folder", "share"));
                
            defaults.put("version-service", 
                List.of("version", "repository", "commit", "branch"));
                
            // 5. 通信服务默认路由
            defaults.put("chat-service", 
                List.of("chat", "message", "room"));
                
            defaults.put("ai-chat-service", 
                List.of("aiChat", "modelConfig", "ai", "model"));
                
            // 6. 工具服务默认路由
            defaults.put("task-service", 
                List.of("task", "schedule", "reminder"));
                
            return defaults;
        }
        
        return serviceRoutes;
    }

    /**
     * 配置限流的键解析器，此解析器基于客户端的IP地址进行限流
     * 主要用于控制每个IP地址对服务的访问频率，防止某个IP的请求过多，影响服务稳定性
     *
     * @return KeyResolver接口的实现，用于解析限流的键值
     */
    @Bean
    public KeyResolver remoteAddrKeyResolver() {
        // 解析并返回客户端的IP地址作为限流的键
        return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostString());
    }
}
